package com.android.kalaha;

import java.util.ArrayList;

import android.widget.Button;
import android.content.res.Resources;

/**
 * Class Player
 * @author nik
 *
 */
public class Player extends Global{
	private Button button;
	private Integer actor;
	private Resources resources;
	private Integer id;
	private Integer level;
	
	public Boolean has_won = false;
	public Boolean has_draw = false;
	
	public ArrayList<Cup> cups  = new ArrayList<Cup>();
	private Cup can;
	
	/**
	 * Constructor
	 * @param button
	 * @param id
	 */
	public Player(Button button, Integer id){
		this.button = button;
		this.id = id;
	
		if(this.button!=null){
			this.resources = this.button.getContext().getResources();
		}
	}

	/**
	 * Add a cup to the players cup collection
	 * @param cup
	 */
	public void add_cup(Cup cup){
		this.cups.add(cup);
	}

	/**
	 * Check if the given cup belongs to the player
	 * @param cup
	 * @return Boolean
	 */
	public boolean is_owner_of_cup(Cup cup){
		this.debug("try to find out if it's the owner of cup");
		return this.cups.contains(cup);
	}
	
	/**
	 * Change the "actor" of the player. (Who is playing as this player)
	 * And adapt the layout of the player to the current actor.
	 * Set also the difficulty level of the actor. 
	 */
	public void set_actor(String actor){
		
		Integer new_actor = Integer.parseInt(actor);
		this.set_actor(new_actor);
		
	}
		
	public void set_actor(Integer actor){
		
		this.actor = actor;
	
		if(this.actor==0){
			this.set_level(this.default_actor_difficulty);
		}else{
			this.set_level(this.actor);
		}

		boolean adapt_button = (this.button!=null);
		if(adapt_button){
			switch(this.actor){
			case 0:
				this.button.setBackgroundResource(R.drawable.human);
				break;
			case 1:
				this.button.setBackgroundResource(R.drawable.android);
				break;
			case 2:
				this.button.setBackgroundResource(R.drawable.android);
				break;
			case 3:
				this.button.setBackgroundResource(R.drawable.android);
				break;
			case 4:
				this.button.setBackgroundResource(R.drawable.android);
				break;
			case 5:
				this.button.setBackgroundResource(R.drawable.android);
				break;
			case 6:
				this.button.setBackgroundResource(R.drawable.android);
				break;
			default:
				this.button.setBackgroundResource(R.drawable.human);
				break;
			}
			this.redraw();
		}
	}
		
	/**
	 * Is this player an android or a human.
	 * @return Boolean 
	 */
	public boolean is_android(){
		return (this.actor!=0);
	}

	/**
	 * Enable the Button
	 */
	public void activate(){
		if(this.button==null){
			return;
		}	

		this.button.setEnabled(true);
	}
	

	/**
	 * Disable the player: No onClick Event
	 */
	public void deactivate(){
		if(this.button==null){
			return;
		}
		this.button.setEnabled(false);
	}

	/**
	 * Redraw the player with the current properties.
	 */
	public void redraw(){
		String[] labels = this.resources.getStringArray(R.array.preference_actors_label);
		this.button.setText(labels[this.actor] + " (" + this.level.toString() + ")");
	}

	/**
	 * Reset the Player
	 */
	public void reset(){
		this.has_won = false;
		this.has_draw = false;
	}

	/**
	 * @return the level
	 */
	public Integer get_level() {
		return level;
	}

	/**
	 * @param the level to set
	 */
	public void set_level(Integer level) {
		this.level = level;
	}	

	/**
	 * @param
	 */
	public String get_label(){
		Integer nr = this.id+1;
		return "Player " + nr.toString();
	}

	/**
	 * @return the can
	 */
	public Cup get_can() {
		return can;
	}

	/**
	 * @param the can to set
	 */
	public void set_can(Cup can) {
		this.can = can;
	}

	/**
	 * @return the id
	 */
	public Integer get_id() {
		return id;
	}

	/**
	 * @param the id to set
	 */
	public void set_id(Integer id) {
		this.id = id;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Player [actor=" + actor + ", id=" + id + ", level=" + level
				+ ", has_won=" + has_won + ", has_draw=" + has_draw + "]";
	}

}